<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<style>
    body{background-color: #FFFFFF}
    .layui-fluid{padding: 30px 0!important;}
    .change_page{background-color: #FFFFFF}
    td{padding: 0!important;}
    .xuan{text-align: center;}
    .xuanname{padding-left: 10px}
    .disabled{background-color: #ccc!important;}
</style>

<body id="optPage" style="overflow-y: hidden;">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title">
        <div class="page-title-con">
            <span id="flowsetname">设置成本合价信息项</span>
            <div class="move_title"></div>
            <div class="page-title-opt-list">
                <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" lay-submit="" lay-filter="add">提交</button>
                <span id="page-close" class="page-close" onclick="layer_close()"><i class="layui-icon layui-icon-close"></i></span>
            </div>
        </div>
    </div>
    <div class="main-container" style="0 20px 0 20px !important">

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>设置成本合价信息项</span></div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th>名称</th>
                            <th  style="text-align: center"><input type="checkbox" class="xuanhang xuanhang1" lay-filter="xuanhang1" lay-skin="primary">人工费</th>
                            <th  style="text-align: center"><input type="checkbox" class="xuanhang xuanhang4" lay-filter="xuanhang4" lay-skin="primary">材料费</th>
                            <th  style="text-align: center"><input type="checkbox" class="xuanhang xuanhang3" lay-filter="xuanhang3" lay-skin="primary">机械费</th>
                            <th  style="text-align: center"><input type="checkbox" class="xuanhang xuanhang2" lay-filter="xuanhang2" lay-skin="primary">分包费</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list" style="max-height: 500px"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>


    </div>
</form>
<script>
    var basallist = parent.basallist;
    var basalarr = parent.basalarr;
    var hang_key = getQueryVariable('key');
    var table_type = getQueryVariable('type');


    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        var chengStrObj;
        var chengStr =  $("#list_list"+table_type+" .chengObj" , parent.document).eq(hang_key).text() || '';
        if(chengStr){
            chengStrObj = JSON.parse(chengStr);
        }else{
            chengStrObj = '';
        }

        // 当前分类下其他条和其他分类所有
        var otherObj = get_other_obj();
        var str = '';

        for (var key in basallist) {
            var nums = parseInt(key)+1;
            str += '<tr key="qing">';
            str += '<td style="width: 45px">'+nums+'</td>';
            str += '<td><input readonly type="text" value="'+basallist[key]+'" class="layui-input name"></td>';

            var disabled1 = '';
            var disabled2 = '';
            var disabled3 = '';
            var disabled4 = '';
            if(str_is_join_strs(basallist[key],otherObj[1])){
                disabled1 = 'disabled';
            }
            if(str_is_join_strs(basallist[key],otherObj[2])){
                disabled2 = 'disabled';
            }
            if(str_is_join_strs(basallist[key],otherObj[3])){
                disabled3 = 'disabled';
            }
            if(str_is_join_strs(basallist[key],otherObj[4])){
                disabled4 = 'disabled';
            }

            if(chengStrObj[1]){
                if(str_is_join_strs(basallist[key],chengStrObj[1])){
                    str += '<td  class="xuan '+disabled1+'"><input type="checkbox" '+disabled1+' class="xuanhang xuanhang1" checked lay-skin="primary"></td>';
                }else{
                    str += '<td  class="xuan '+disabled1+'"><input type="checkbox" '+disabled1+' class="xuanhang xuanhang1" lay-skin="primary"></td>';
                }
            }else{
                str += '<td  class="xuan '+disabled1+'"><input type="checkbox" '+disabled1+' class="xuanhang xuanhang1" lay-skin="primary"></td>';
            }


            if(chengStrObj[4]){
                if(str_is_join_strs(basallist[key],chengStrObj[4])){
                    str += '<td  class="xuan '+disabled4+'"><input type="checkbox" '+disabled4+' class="xuanhang xuanhang4" checked lay-skin="primary"></td>';
                }else{
                    str += '<td  class="xuan '+disabled4+'"><input type="checkbox" '+disabled4+' class="xuanhang xuanhang4" lay-skin="primary"></td>';
                }
            }else{
                str += '<td  class="xuan '+disabled4+'"><input type="checkbox" '+disabled4+' class="xuanhang xuanhang4" lay-skin="primary"></td>';
            }
            if(chengStrObj[3]){
                if(str_is_join_strs(basallist[key],chengStrObj[3])){
                    str += '<td  class="xuan '+disabled3+'"><input type="checkbox" '+disabled3+' class="xuanhang xuanhang3" checked lay-skin="primary"></td>';
                }else{
                    str += '<td  class="xuan '+disabled3+'"><input type="checkbox" '+disabled3+' class="xuanhang xuanhang3" lay-skin="primary"></td>';
                }
            }else{
                str += '<td  class="xuan '+disabled3+'"><input type="checkbox" '+disabled3+' class="xuanhang xuanhang3" lay-skin="primary"></td>';
            }
            if(chengStrObj[2]){
                if(str_is_join_strs(basallist[key],chengStrObj[2])){
                    str += '<td  class="xuan '+disabled2+'"><input type="checkbox" '+disabled2+' class="xuanhang xuanhang2" checked lay-skin="primary"></td>';
                }else{
                    str += '<td  class="xuan '+disabled2+'"><input type="checkbox" '+disabled2+' class="xuanhang xuanhang2" lay-skin="primary"></td>';
                }
            }else{
                str += '<td  class="xuan '+disabled2+'"><input type="checkbox" '+disabled2+' class="xuanhang xuanhang2" lay-skin="primary"></td>';
            }
            str += '</tr>';
        }

        $("#list_list").append(str);
        render_from();
        update_refresh_row_func();

        form.on('checkbox(xuanhang1)',function (data) {
            if($(data.elem).prop("checked")){
                $('#list_list .xuanhang1').prop("checked", true);
            }else{
                $('#list_list .xuanhang1').prop("checked", false);
            }
            render_from();
        });
        form.on('checkbox(xuanhang2)',function (data) {
            if($(data.elem).prop("checked")){
                $('#list_list .xuanhang2').prop("checked", true);
            }else{
                $('#list_list .xuanhang2').prop("checked", false);
            }
            render_from();
        });
        form.on('checkbox(xuanhang3)',function (data) {
            if($(data.elem).prop("checked")){
                $('#list_list .xuanhang3').prop("checked", true);
            }else{
                $('#list_list .xuanhang3').prop("checked", false);
            }
            render_from();
        });
        form.on('checkbox(xuanhang4)',function (data) {
            if($(data.elem).prop("checked")){
                $('#list_list .xuanhang4').prop("checked", true);
            }else{
                $('#list_list .xuanhang4').prop("checked", false);
            }
            render_from();
        });


        form.on('submit(add)', function(data){
            var chengStr1 = '';
            var chengStr2 = '';
            var chengStr3 = '';
            var chengStr4 = '';
            var money = 0; // 外面的统计数据

            $('#list_list tr').each(function (i) {
                var name = $(this).find('.name').val();
                var checked1 = $(this).find('.xuanhang1').prop("checked");

                if(checked1 && $(this).find('.xuanhang1').parent().attr('class') != 'xuan disabled'){
                    if(chengStr1){
                        chengStr1 += ','+name;
                    }else{
                        chengStr1 += name;
                    }

                    for (var i=0;i<basalarr.length;i++){
                        if(basalarr[i].fenname == name){
                            money += parseFloat(basalarr[i].renfee_cur);
                        }
                    }
                }

                var checked2 = $(this).find('.xuanhang2').prop("checked");
                if(checked2 && $(this).find('.xuanhang2').parent().attr('class') != 'xuan disabled'){
                    if(chengStr2){
                        chengStr2 += ','+name;
                    }else{
                        chengStr2 += name;
                    }

                    for (var i=0;i<basalarr.length;i++){
                        if(basalarr[i].fenname == name){
                            money += parseFloat(basalarr[i].fenfee_cur);
                        }
                    }
                }

                var checked3 = $(this).find('.xuanhang3').prop("checked");
                if(checked3 && $(this).find('.xuanhang3').parent().attr('class') != 'xuan disabled'){
                    if(chengStr3){
                        chengStr3 += ','+name;
                    }else{
                        chengStr3 += name;
                    }

                    for (var i=0;i<basalarr.length;i++){
                        if(basalarr[i].fenname == name){
                            money += parseFloat(basalarr[i].jifee_cur);
                        }
                    }
                }

                var checked4 = $(this).find('.xuanhang4').prop("checked");
                if(checked4 && $(this).find('.xuanhang4').parent().attr('class') != 'xuan disabled'){
                    if(chengStr4){
                        chengStr4 += ','+name;
                    }else{
                        chengStr4 += name;
                    }

                    for (var i=0;i<basalarr.length;i++){
                        if(basalarr[i].fenname == name){
                            money += parseFloat(basalarr[i].caifee_cur);
                        }
                    }
                }
            });

            var chengObj = {
                1: chengStr1,
                2: chengStr2,
                3: chengStr3,
                4: chengStr4,
            };

            if(chengStr1 || chengStr2 || chengStr3 || chengStr4){
                $("#list_list"+table_type, parent.document).find('.chanbut').eq(hang_key).addClass('buttonlv').removeClass('buttonnone');
                var chengObjStr = JSON.stringify(chengObj);
                $("#list_list"+table_type, parent.document).find('.chengObj').eq(hang_key).text(chengObjStr);

                $("#list_list"+table_type, parent.document).find('.money').eq(hang_key).text(money);
            }else{
                $("#list_list"+table_type, parent.document).find('.chanbut').eq(hang_key).addClass('buttonnone').removeClass('buttonlv');
                $("#list_list"+table_type, parent.document).find('.chengObj').eq(hang_key).text('');

                $("#list_list"+table_type, parent.document).find('.money').eq(hang_key).text('');
            }

            layer_close();
            return false;
        });

    });





    function get_other_obj() {
        var existStr1 = '';
        var existStr2 = '';
        var existStr3 = '';
        var existStr4 = '';

        var other_table_type = [];
        if(table_type == 1){
            other_table_type = [2,3,4];
        }else if(table_type == 2){
            other_table_type = [1,3,4];
        }else if(table_type == 3){
            other_table_type = [1,2,4];
        }else if(table_type == 4){
            other_table_type = [1,2,3];
        }

        $.each($("#list_list"+table_type+" .chengObj" , parent.document),function (key, val) {
            if(key != hang_key){
                var str =  $(val).text() || '';
                if(str){
                    var Obj = JSON.parse(str);
                }else{
                    var Obj = '';
                }
                if(Obj[1]){
                    if(existStr1){
                        existStr1 += ','+Obj[1];
                    }else{
                        existStr1 += Obj[1];
                    }
                }
                if(Obj[2]){
                    if(existStr2){
                        existStr2 += ','+Obj[2];
                    }else{
                        existStr2 += Obj[2];
                    }
                }
                if(Obj[3]){
                    if(existStr3){
                        existStr3 += ','+Obj[3];
                    }else{
                        existStr3 += Obj[3];
                    }
                }
                if(Obj[4]){
                    if(existStr4){
                        existStr4 += ','+Obj[4];
                    }else{
                        existStr4 += Obj[4];
                    }
                }
            }
        });

        other_table_type.forEach(function (val) {
            $.each($("#list_list"+val+" .chengObj" , parent.document),function (key, val) {
                var str =  $(val).text() || '';
                if(str){
                    var Obj = JSON.parse(str);
                }else{
                    var Obj = '';
                }
                if(Obj[1]){
                    if(existStr1){
                        existStr1 += ','+Obj[1];
                    }else{
                        existStr1 += Obj[1];
                    }
                }
                if(Obj[2]){
                    if(existStr2){
                        existStr2 += ','+Obj[2];
                    }else{
                        existStr2 += Obj[2];
                    }
                }
                if(Obj[3]){
                    if(existStr3){
                        existStr3 += ','+Obj[3];
                    }else{
                        existStr3 += Obj[3];
                    }
                }
                if(Obj[4]){
                    if(existStr4){
                        existStr4 += ','+Obj[4];
                    }else{
                        existStr4 += Obj[4];
                    }
                }
            });
        });

        var existObj = {
            1: existStr1,
            2: existStr2,
            3: existStr3,
            4: existStr4,
        };
        return existObj;
    }


    function str_is_join_strs(sea,str) {
        let arr = [];
        arr = str.split(",");
        for(var i in arr){
            if(arr[i]==sea){
                return true;
            }
        }
        return false;
    }
</script>
</body>
</html>
